package com.xxx.service.impl;

import com.xxx.mapper.ClassInfoMapper;
import com.xxx.mapper.DishMapper;
import com.xxx.pojo.Dish;
import com.xxx.service.Services;
import com.xxx.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class DishServiceImpl implements Services<Dish> {

    public SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();

    @Override
    public Dish selectByUsername(String dishName) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DishMapper mapper = sqlSession.getMapper(DishMapper.class);
        Dish dish = mapper.selectByName(dishName);
        sqlSession.close();
        return dish;
    }

    @Override
    public List<Dish> selectAll(Integer index, Integer num) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DishMapper mapper = sqlSession.getMapper(DishMapper.class);
        List<Dish> dishes = mapper.selectAll(index, num);
        sqlSession.close();

        return dishes;
    }

    @Override
    public int add(Dish dish) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DishMapper mapper = sqlSession.getMapper(DishMapper.class);

        int i = mapper.addDish(dish);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }


    @Override
    public int delete(Long id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DishMapper mapper = sqlSession.getMapper(DishMapper.class);
        int i = mapper.deleteById(id);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public Dish selectById(Long id) {
        return null;
    }

    @Override
    public int updateById(Dish dish) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DishMapper mapper = sqlSession.getMapper(DishMapper.class);
        int i = mapper.updateDish(dish);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int selectCount() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DishMapper mapper = sqlSession.getMapper(DishMapper.class);
        int i = mapper.selectCount();
        sqlSession.close();
        return i;
    }

    @Override
    public List<String> selectAllByTypesName() {
        return null;
    }


}
